package com.china08.yxyapi.service.manage.edu.platform.statistics.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.service.manage.edu.platform.statistics.StatisticsSchoolService;
@Service
public class StatisticsSchoolServiceTwoImpl implements StatisticsSchoolService {
	@Autowired
	private ElasticsearchTemplate et;
	
	@Override
	public List<Long> statisticsResourceCount(String schoolId) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder.mustNot(builderType);
		TermQueryBuilder builderDel = new TermQueryBuilder("uploaderSchoolId", schoolId);
		boolQueryBuilder.must(builderDel);
		TermQueryBuilder builderDel1 = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder.must(builderDel1);
//		TermQueryBuilder builderDel22 = new TermQueryBuilder("viewScope", 0);
//		boolQueryBuilder.mustNot(builderDel22);
		SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();

		// 查询出来的总数量
		long allCount = et.count(query,ElasticYxyResResource.class);
		List<Long> longCount = new ArrayList<Long>();
		longCount.add(allCount);
		
		
		BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
		//查出来的是当前人所在学校的总数量
		TermQueryBuilder builderType2 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder1.mustNot(builderType2);
		TermQueryBuilder builderDel11 = new TermQueryBuilder("uploaderSchoolId", schoolId);
		boolQueryBuilder1.must(builderDel11);
//		TermQueryBuilder builderDel111 = new TermQueryBuilder("viewScope", 0);
//		boolQueryBuilder1.mustNot(builderDel111);
		
		TermQueryBuilder builderDelete = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder1.must(builderDelete);
		SearchQuery	query2 = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder1).build();
		long areaCount = et.count(query2,ElasticYxyResResource.class);
		longCount.add(areaCount);
		
		
		BoolQueryBuilder boolQueryBuilder11 = new BoolQueryBuilder();
		//查出来的是最近新增的资源总数量
		// 统计近30天新增加的资源数量 uploadTime
		Date currentDate = new Date();
		// 30天的毫秒数
		long threeDate = 2592000000L;
		RangeQueryBuilder rangeDate = new RangeQueryBuilder("uploadTime");
		rangeDate.gt(currentDate.getTime() - threeDate);
		boolQueryBuilder11.filter(rangeDate);
		TermQueryBuilder builderType3 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder11.mustNot(builderType3);
		TermQueryBuilder builderDel2 = new TermQueryBuilder("uploaderSchoolId", schoolId);
		boolQueryBuilder11.must(builderDel2);
		TermQueryBuilder builderDel33 = new TermQueryBuilder("viewScope", 0);
		boolQueryBuilder11.mustNot(builderDel33);
		TermQueryBuilder builderDelete1 = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder11.must(builderDelete1);
		SearchQuery query3=new NativeSearchQueryBuilder().withQuery(boolQueryBuilder11).build();
		long nearCount = et.count(query3,ElasticYxyResResource.class);
		longCount.add(nearCount);
		return longCount;
	}
	
	@Override
	public List<Long> statisticsWeikeCount(String schoolId) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder.must(builderType);
		TermQueryBuilder builderDel = new TermQueryBuilder("uploaderSchoolId", schoolId);
		boolQueryBuilder.must(builderDel);
		TermQueryBuilder builderDelete = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder.must(builderDelete);
		/*TermQueryBuilder builderDel111 = new TermQueryBuilder("viewScope", 0);
		boolQueryBuilder.mustNot(builderDel111);*/
		SearchQuery query = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder).build();

		// 查询出来的总数量
		long allCount = et.count(query,ElasticYxyResResource.class);
		List<Long> longCount = new ArrayList<Long>();
		longCount.add(allCount);
		
		BoolQueryBuilder boolQueryBuilder1 = new BoolQueryBuilder();
		//查出来的是当前人所在学校的总数量
		TermQueryBuilder builderType2 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder1.must(builderType2);
		TermQueryBuilder builderDel1 = new TermQueryBuilder("uploaderSchoolId", schoolId);
		boolQueryBuilder1.must(builderDel1);
		/*TermQueryBuilder builderDel11 = new TermQueryBuilder("viewScope", 0);
		boolQueryBuilder1.mustNot(builderDel11);*/
		TermQueryBuilder builderDeleteSchool = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder1.must(builderDeleteSchool);
		SearchQuery	query2 = new NativeSearchQueryBuilder().withQuery(boolQueryBuilder1).build();
		long areaCount = et.count(query2,ElasticYxyResResource.class);
		longCount.add(areaCount);
		
		
		BoolQueryBuilder boolQueryBuilder11 = new BoolQueryBuilder();
		//查出来的是最近新增的资源总数量
		// 统计近30天新增加的资源数量 uploadTime
		Date currentDate = new Date();
		// 30天的毫秒数
		long threeDate = 2592000000L;
		RangeQueryBuilder rangeDate = new RangeQueryBuilder("uploadTime");
		rangeDate.gt(currentDate.getTime() - threeDate);
		boolQueryBuilder11.filter(rangeDate);
		TermQueryBuilder builderType3 = new TermQueryBuilder("typeId", "yx14");
		boolQueryBuilder11.must(builderType3);
		TermQueryBuilder builderDel2 = new TermQueryBuilder("uploaderSchoolId", schoolId);
		boolQueryBuilder11.must(builderDel2);
		TermQueryBuilder builderDel22 = new TermQueryBuilder("viewScope", 0);
		boolQueryBuilder11.mustNot(builderDel22);
		TermQueryBuilder builderDeleteSchool1 = new TermQueryBuilder("isDelete", 0);
		boolQueryBuilder11.must(builderDeleteSchool1);
		SearchQuery query3=new NativeSearchQueryBuilder().withQuery(boolQueryBuilder11).build();
		long nearCount = et.count(query3,ElasticYxyResResource.class);
		longCount.add(nearCount);
		return longCount;
	}

}
